#include "stm32f4xx.h"
#include "led.h"
#include "beep.h"
#include "key.h"
#include "sys.h"
#include "delay.h"
#include "exti.h"
#include "timer.h"
#include "pwm.h"
#include "motor.h"
#include "usart.h"
#include "dht11.h"
#include "adc.h"
#include <string.h>

#define NUM 5

u_int rank=0;
uint16_t Usart_Data;
unsigned short ch = 'C';

void USART1_IRQHandler(void)
{
   //若是非空，则返回值为1，与RESET（0）判断，不相等则判断为真
   if(USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
   {	
   /* DR读取接受到的数据*/
     Usart_Data = USART_ReceiveData(USART1);	
    //然后把接收到的数据返回送到发送端
     USART_SendData(USART1, Usart_Data);
   }
	 
	//判断为真后，为下次中断做准备，则需要对中断的标志清零
	 USART_ClearITPendingBit(USART1,USART_IT_RXNE);
}

unsigned char rcv_buff[24];
unsigned char n_buff[24];

u_int count=0,i=0;
u_int rcv_flag=0;

//每接受到一个字符产生一次
void USART2_IRQHandler(void)
{
   //若是非空，则返回值为1，与RESET（0）判断，不相等则判断为真
   if(USART_GetITStatus(USART2, USART_IT_RXNE) == SET)
   {	
   /* DR读取接受到的数据*/
//     Usart_Data = USART_ReceiveData(USART2);	
    //然后把接收到的数据返回送到发送端
//     USART_SendData(USART2, Usart_Data);
		 
		 rcv_buff[count++] = USART_ReceiveData(USART2);	
		 
		 if(rcv_buff[count-1]=='#')
		 {
				rcv_flag = 1;
			  i=0;
			  count = 0;
		 }
		 
		if(rcv_flag==0)
				n_buff[i++] = rcv_buff[count-1];
		 
		 
   }
	 
	//判断为真后，为下次中断做准备，则需要对中断的标志清零
	 USART_ClearITPendingBit(USART2,USART_IT_RXNE);
}


int main(void)
{
	 int i=0,ret=0,timerr=0;
	 u_char value[NUM];
	
	 u16 l_value = 0;
	
	 uint8_t ADC_min=0,ADC_max=0;//记录光敏电阻产生的数据中的最大最小值
	 uint8_t ADC_sj=0;			//读取到的光敏电阻ADC数据
	 uint8_t ADC_PWM=0;
	
	 int temp_max = 25;
	 int temp_min = 0;
	 int hum_max = 95;
	 int hum_min = 0;
	 int tag=100;
	
	 ADC_min = ADC_max = l_value;
	
	  //中断分组配置 -- 分为第2组
		NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
		LED_Init();
	  BEEP_Init();
    KEY_Init();
//		Delay_Init();
//  	//Exti_Init();
// 		TIM3_Init();
   	PWM_Init();
  	PWM1_Init();
	  //MOTOR_Init();
	  //USART1_Init();
	  USART2_Init();
	  Adc_Init();
	
	  while(1)
		{
			

			if(rcv_flag==1)
			{
				
				if(strcmp(n_buff,"turn_on")==0)
					GPIO_ResetBits(GPIOE,GPIO_Pin_14);

				if(strcmp(n_buff,"turn_off")==0)
					GPIO_SetBits(GPIOE,GPIO_Pin_14);
				
				if(strcmp(n_buff,"beep_off")==0)
					GPIO_ResetBits(GPIOF,GPIO_Pin_8);

				memset(rcv_buff,0,sizeof(rcv_buff));
				memset(n_buff,0,sizeof(n_buff));
				
				rcv_flag = 0;
			}
			
			
			delay_ms(500);
			timerr++; 
			if(timerr>5000)
				timerr=0;
			
if(timerr%5==0)
{
			ret = DHT11_Start();	
			if(ret==0)
			{
				ret = DHT11_Read(value);
			}
			delay_ms(10);
			//采集光照强度超过设置的最低值或者最高值，蜂鸣器进行报警			
			l_value = Get_Adc_Value();
//			printf("Light:%hd\r\n",l_value);

			printf("Light:%hd,Hum:%d.%d, Temp:%d.%d\r\n",l_value,value[0], value[1], value[2], value[3]);
			
			
}
			
							

//设置温度与湿度的最高或者最低安全值，当超过设置安全值，蜂鸣器报警						
				if(PEin(3)==0)
		    {
			    delay_ms(30);
				  if(PEin(3)==0)
				   {
				    temp_max = temp_max+5;
						hum_max =hum_max+5;
						printf("hum_max:%d temp_max:%d\r\n",hum_max,temp_max);
				   }
		    }
				if(PEin(4)==0)
		    {
			     delay_ms(30);
				   if(PEin(4)==0)
				   {
				    temp_max = temp_max-5;
						hum_max =hum_max-5;
						 printf("hum_max:%d temp_max:%d\r\n",hum_max,temp_max);
				   }
		    }
				
				
			if(value[2]>temp_max||value[0]>hum_max||l_value<1000||l_value>4000)
			{ 
					GPIO_SetBits(GPIOF,GPIO_Pin_8); delay_s(1);
			}
			else{GPIO_ResetBits(GPIOF,GPIO_Pin_8); }

					
					

					
//实时采集仓库光照强度，并可以通过光敏强度调节LED灯亮度						
				ADC_sj = l_value;//采集数据
		    
		    if(ADC_sj < ADC_min)	
				{
					ADC_min = ADC_sj;
				}
		    if(ADC_sj > ADC_max)	
				{
					ADC_max = ADC_sj;
		    }
				
		    ADC_PWM = (ADC_sj - ADC_min)*500/(ADC_max - ADC_min);//计算相对数据
		    TIM_SetCompare1(TIM14,ADC_PWM);

					


			
//通过按键设置LED灯的亮度比例控制
			if(PAin(0)==0)
			{
			  delay_ms(30);
				if(PAin(0)==0)
				{
					tag = tag+100;
					PEout(13) = 1;
					printf("led :%d\r\n",tag);
					TIM_SetCompare3(TIM1,tag);
				}
			}
				if(PEin(2)==0)
			{
			  delay_ms(30);
				if(PEin(2)==0)
				{
					tag = tag-100;
					PEout(13) = 1;
					printf("led :%d\r\n",tag);
					TIM_SetCompare3(TIM1,tag);
				}
			}
			
			
			if(tag>400)
				tag=400;
			if(tag<100)
				tag=100;
			if(hum_max>100||temp_max>60)
			{
				hum_max=100;temp_max=60;
			}	
			if(hum_max<40||temp_max<0)
			{
				hum_max=40;temp_max=0;
			}

		}
	  
	  
	  
   
		return 0;
}






